<template>
  <div class="login-bg">
    <div class="login-card">
      <div class="logo">
        <h1>欢迎回来</h1>
        <p class="sub-title">输入您的账号和密码登录</p>
      </div>

      <el-form :model="form" @submit.prevent="handleLogin" class="login-form">
        <!-- 用户名 -->
        <el-form-item>
          <el-input
            v-model="form.username"
            prefix-icon="User"
            placeholder="请输入用户名"
            size="large"
          />
        </el-form-item>

        <!-- 密码 -->
        <el-form-item>
          <el-input
            v-model="form.password"
            prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
            size="large"
            show-password
          />
        </el-form-item>

        <!-- 滑块验证码占位（可接入第三方） -->
        <div class="slider-captcha">
          <span>按住滑块拖动</span>
        </div>

        <!-- 记住密码 + 忘记密码 -->
        <div class="form-row">
          <el-checkbox v-model="form.rememberMe">记住密码</el-checkbox>
          <el-link type="primary" @click="$router.push('/forgot')"> 忘记密码？ </el-link>
        </div>

        <!-- 登录按钮 -->
        <el-button
          type="primary"
          size="large"
          native-type="submit"
          :loading="loading"
          class="login-btn"
        >
          {{ loading ? loadingText : '登录' }}
        </el-button>

        <!-- 连接状态提示 -->
        <div v-if="connectionStatus" class="connection-status" :class="connectionStatus.type">
          <i :class="connectionStatus.icon"></i>
          {{ connectionStatus.message }}
        </div>

        <!-- 注册提示 -->
        <div class="register-tip">
          还没有账号？<el-link type="primary" @click="toRegister">注册</el-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'

const form = ref({
  username: '',
  password: '',
  rememberMe: false,
})

const loading = ref(false)
const loadingText = ref('登录中...')
const connectionStatus = ref(null)
const router = useRouter()
const userStore = useUserStore()
const toRegister = () => router.push('/register')

const handleLogin = async () => {
  if (!form.value.username || !form.value.password) {
    ElMessage.warning('请输入账号密码')
    return
  }

  loading.value = true
  connectionStatus.value = null

  try {
    // 设置登录状态提示
    loadingText.value = '连接服务器中...'
    connectionStatus.value = {
      type: 'info',
      icon: 'el-icon-loading',
      message: '正在验证账号信息...',
    }

    const startTime = Date.now()

    // 调用store的login方法，它会自动处理状态设置
    const result = await userStore.login(
      form.value.username,
      form.value.password,
      form.value.rememberMe,
    )

    const responseTime = Date.now() - startTime
    console.log(`✅ 登录完成，响应时间: ${responseTime}ms`)

    // 更新状态为成功
    loadingText.value = '登录成功'
    connectionStatus.value = {
      type: 'success',
      icon: 'el-icon-check',
      message: `登录成功 (${responseTime}ms)`,
    }

    ElMessage.success('登录成功')

    // 短暂延迟以显示成功状态，然后跳转
    setTimeout(async () => {
      await router.push('/')
    }, 500)
  } catch (e) {
    console.error('登录失败:', e)

    // 更新状态为失败
    connectionStatus.value = {
      type: 'error',
      icon: 'el-icon-close',
      message: e.message || '登录失败',
    }

    ElMessage.error(e.message || '登录失败')

    // 3秒后清除错误状态
    setTimeout(() => {
      connectionStatus.value = null
    }, 3000)
  } finally {
    loading.value = false
    loadingText.value = '登录中...'
  }
}
</script>
<style scoped>
/* 背景图（替换成自己的） */
.login-bg {
  height: 100vh;
  /* background: url('../img/34.png') ; */
  background-color: rgb(33, 96, 214);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

/* 毛玻璃卡片 */
.login-card {
  width: 420px;
  padding: 50px 40px;
  /* background: rgba(255, 255, 255, 0.25); */
  background-color: aliceblue;
  backdrop-filter: blur(12px);
  border-radius: 24px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
  text-align: center;
}

.logo-img {
  width: 64px;
  margin-bottom: 8px;
}

h1 {
  margin: 0;
  font-size: 28px;
  color: #110e0e;
  font-weight: 600;
}

.sub-title {
  margin: 4px 0 24px;
  font-size: 14px;
  color: rgba(27, 22, 22, 0.8);
}

/* 表单 */
.login-form {
  text-align: left;
}

.form-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

/* 滑块验证码 */
.slider-captcha {
  height: 40px;
  background: rgba(36, 32, 32, 0.2);
  border-radius: 20px;
  color: #524242;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  cursor: pointer;
  margin-bottom: 20px;
}

/* 登录按钮 */
.login-btn {
  width: 100%;
  height: 46px;
  border-radius: 23px;
  font-size: 16px;
  font-weight: 600;
}

/* 连接状态提示 */
.connection-status {
  margin-top: 12px;
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 13px;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: all 0.3s ease;
}

.connection-status.info {
  background: rgba(64, 158, 255, 0.1);
  color: #409eff;
  border: 1px solid rgba(64, 158, 255, 0.2);
}

.connection-status.success {
  background: rgba(103, 194, 58, 0.1);
  color: #67c23a;
  border: 1px solid rgba(103, 194, 58, 0.2);
}

.connection-status.error {
  background: rgba(245, 108, 108, 0.1);
  color: #f56c6c;
  border: 1px solid rgba(245, 108, 108, 0.2);
}

.connection-status i {
  font-size: 14px;
}

/* 注册提示 */
.register-tip {
  margin-top: 16px;
  font-size: 14px;
  color: rgba(24, 21, 21, 0.85);
}

/* 移动端 */
@media (max-width: 480px) {
  .login-card {
    width: 90%;
    padding: 32px 24px;
  }
}
</style>
